package com.woniuxy.controller;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.woniuxy.entity.Activity;
import com.woniuxy.entity.Img;
import com.woniuxy.service.ActiveService;
import com.woniuxy.util.Page;
import com.woniuxy.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("active")
public class ActiveController {

    @Resource
    private ActiveService activeService;

    @GetMapping("selectAllCampAndTypes")
    public Result selectAllCampAndTypes() throws Exception{
        return new Result("200", "success", null, activeService.selectAllCampAndTypes());
    }
    @GetMapping("selectAllActive")
    public Result selectAllActive(Page page,Integer campId,Integer typeId,Integer status) throws Exception{
        int count = activeService.selectCount(campId,typeId,status);
        int pageCount=count % page.getNumber() == 0 ? count / page.getNumber() : count / page.getNumber() + 1;
        page.setCount(count);
        page.setPageCount(pageCount);

        return new Result("200", "success", page, activeService.selectAllActive(page,campId,typeId,status));
    }
    @PostMapping("insertActive")
    public Result addActive(Activity activity,String[] url) throws Exception {
        activeService.insertActivity(activity,url);
        return new Result("","success","null",null);
    }

    @DeleteMapping("deleteActive/{actId}")
    public Result deleteActActive(@PathVariable("actId") Integer actId) throws Exception{
        activeService.deleteActActiveById(actId);
        return new Result("","success","null",null);
    }

    @PostMapping("updateActive")
    public Result updateActive(Activity activity,Integer[] deleteImgId,String[] url) throws Exception{
        activeService.updateActiveById(activity,deleteImgId,url);
        return new Result("","success","null",null);
    }

    @GetMapping("selectAllActOrderCampAndTypes")
    public Result selectAllActOrderCampAndTypes(Page page,Integer actId,Integer status,Integer campId) throws Exception{

        int count = activeService.selectOrderCount(campId,actId,status);
        int pageCount=count % page.getNumber() == 0 ? count / page.getNumber() : count / page.getNumber() + 1;
        page.setCount(count);
        page.setPageCount(pageCount);
        return new Result("","success",page,activeService.selectAllActOrderCampAndTypes(page,campId,actId,status));
    }











    @Autowired
    private StringRedisTemplate redisTemplate;

    @GetMapping("test")
    public void insertRedis(){
        ValueOperations forValue = redisTemplate.opsForValue();
        forValue.set("test1","123",100000,TimeUnit.MILLISECONDS);
        forValue.set("test1","123123",100000,TimeUnit.MILLISECONDS);
        System.out.println(forValue.get("test1"));
    }


}
